package cn.jeasy.core.context;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



@WebListener
public class ContextLoaderListener  implements ServletContextListener {
	
	 private static final Logger logger = LoggerFactory.getLogger(ContextLoaderListener.class);
	 private static String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE="contextConfigLocation";
	 private static String ROOT_WEB_APPLICATION_CONTEXT_FILE="applicationContext.xml";
	 //private  ApplicationContext applicationContext;
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try {			
			String attr=sce.getServletContext().getInitParameter(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
			if(attr!=null){
				ROOT_WEB_APPLICATION_CONTEXT_FILE=attr;				
			}
			new ClassPathXmlApplicationContext(ROOT_WEB_APPLICATION_CONTEXT_FILE);
		} catch (Exception e) {		
			e.printStackTrace();
		}
		logger.debug("ContextLoaderListener Initialized");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
		logger.debug("ContextLoaderListener contextDestroyed");
	}

	
}